from django.contrib.auth.models import User
from django.db import models

# Create your models here.
class UserDetails(models.Model):
    SEX_CHOICES = (
        (0,"女"),
        (1,"男"),
    )
    age = models.IntegerField(null=True,blank=True) #年龄
    sex = models.IntegerField(null=True,blank=True,choices=SEX_CHOICES) #性别
    signature = models.TextField(null=True,blank=True,default='加油')  #个性签名
    cover = models.TextField(default="index.png")
    target_start_date = models.DateField(null=True,blank=True)
    target_end_date = models.DateField(null=True,blank=True)
    target = models.TextField(null=True,blank=True,default="请设置目标")
    plan = models.TextField(null=True,blank=True,default="请设置计划")
    user = models.OneToOneField(User,on_delete=models.CASCADE)

class Follow(models.Model):
    follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='follower')#关注者
    following = models.ForeignKey(User, on_delete=models.CASCADE, related_name='following')#被关注者
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = (('follower', 'following'),)

    def __str__(self):
        return f'{self.follower} {self.following}'

class Messages(models.Model):
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sent_messages')
    receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='received_messages')
    content = models.TextField()
    sent_at = models.DateTimeField(auto_now_add=True)
    read = models.BooleanField(default=False)
    deleted_by_sender = models.BooleanField(default=False)
    deleted_by_receiver = models.BooleanField(default=False)

    class Meta:
        ordering = ['sent_at']

    def __str__(self):
        return f"{self.sender} to {self.receiver}: {self.content[:20]}"
